package com.wz.autoconfig;

import com.wz.token.TokenContext;
import com.wz.token.TokenProperties;
import com.wz.token.config.TokenInterceptorConfig;
import com.wz.token.interceptor.TokenInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(TokenProperties.class)
@ConditionalOnProperty(
        prefix = "wz.token",
        name = "enable",
        havingValue = "true",
        matchIfMissing = false
)
public class TokenAutoConfigure {

    TokenProperties properties;

    public TokenAutoConfigure(TokenProperties properties) {
        this.properties = properties;
    }

    /**
     * 当配置类生效时 向ioc容器注册token
     *
     * @return
     */
    @Bean
    public TokenContext tokenContext() {
        return new TokenContext(properties);
    }

    @Bean
    public TokenInterceptorConfig TokenInterceptorConfig(TokenContext tokenContext) {
        TokenInterceptorConfig config = new TokenInterceptorConfig();
        config.setTokenInterceptor(new TokenInterceptor(tokenContext));
        config.setProperties(properties);
        return config;
    }


}